<?php
/**
 * Date: 6/14/12
 * Time: 1:21 AM
 */
require_once '../bootstrap.php';
include_once '../includes/formvalidator.php';
include_once '../includes/utils.php';

// Must login
if (empty($_SESSION['admin_id'])) {
	header('Location: login.php');
}

$__page = "ads";

$ads_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$selected_ads = $my_db->select_row('SELECT * FROM ads WHERE adID = ?', array($ads_id));

if (isset($_GET['id']) && empty($selected_ads)) {
	echo 'There are no such league in our system!';
} else {
	$data = $selected_ads;
	if (isset($_POST['Submit'])){
		$data = $_POST;
		// Validation
		$validations = array(
			'url' => array('type' => 'url', 'message' => 'You must enter a valid url'),
			'name' => array('type' => 'alfanum', 'message' => 'You must enter a valid name (Alpha Numeric)'),
			'desc' => array('type' => 'anything', 'message' => 'You must enter a valid description'),
			'zipcode' => array('type' => 'anything', 'message' => 'You must enter a valid Zipcode (Numeric)'),
			'email' => array('type' => 'email', 'message' => 'You must enter a valid email address'),
		);
		// Required array
		$required = array('url', 'name', 'desc', 'zipcode', 'email' );
		$sanatize = array();
		foreach ($validations as $item_name => $options) {
			switch ($options['type']) {
				case 'anything':
					array_push($sanatize, $item_name);
					break;
				case 'number':
					$sanatize[$item_name] = 'int';
					break;
				case 'price':
					$sanatize[$item_name] = 'float';
					break;
				case 'email':
					$sanatize[$item_name] = 'email';
					break;
				default:
					break;
			}
		}
		$validator = new FormValidator($validations, $required, $sanatize);

		if ($validator->validate($data)) {
			// Very important!!
			$data = $validator->sanatize($data);
			$data_table = array($data['url'], $data['name'], $data['desc'], $data['zipcode'], $data['email']);
			if (!empty($selected_league)) {
				// ACTION = EDIT
				$data_table [] = $_GET['id'];
				$return_code = $my_db->execute('UPDATE ads SET `url` = ?, `name` = ?, `desc` =?, `zipcode` = ?, `email` = ? WHERE id = ?', $data_table);
			} else {
				// ACTION = ADD
				$return_code = $my_db->execute('INSERT ads (`link`,`name`,`descr`,`zipcode`,`email`) VALUES (?, ?, ?, ?, ?)', $data_table);
			}

			if ($return_code > 0) {
				$mess = getADMSuccess("League have been updated.");
				$data = array();
			}

		} else {
			$form_errors = $validator->getError();

			if (!empty($form_errors)) {
				foreach ($form_errors as $error) {
					$errors .= li($error);
				}
				$mess = getADMError($errors);
			}
		}
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="X-UA-Compatible" content="IE=7"/>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"/>
	<title><?php echo $title; ?></title>

	<style type="text/css" media="all">
		@import url("css/style.css");
		@import url("css/jquery.wysiwyg.css");
		@import url("css/facebox.css");
		@import url("css/visualize.css");
		@import url("css/date_input.css");
	</style>

	<!--[if lt IE 8]>
	<style type="text/css" media="all">@import url("css/ie.css");</style><![endif]-->
	<link rel="stylesheet" href="css/jquery-ui.css" type="text/css" media="screen" />
	<script language="javascript" src="js/jquery.min.js"></script>
	<script language="javascript" src="js/jquery.ui.core.min.js"></script>
	<script language="javascript" src="js/jquery.ui.datepicker.min.js"></script>
	<script language="javascript" src="js/validate.js"></script>

</head>


<body>

<div id="hld">

<div class="wrapper">        <!-- wrapper begins -->

<?php include("__menu.php");?>
<!-- #header ends -->
<!-- .block ends -->
<div class="block">
<div class="block_head">
	<div class="bheadl"></div>
	<div class="bheadr"></div>
	<h2>Add Edit Ads</h2>
</div>
<!-- .block_head ends -->
<div class="block_content">
<?php echo $mess;?>
<p>
	<form action="" name="f1" id="f1" method="post" class="theForm" onsubmit="return validate();">
		<table width="100%" border="0" cellspacing="2" cellpadding="2">
			<tr>
				<td width="38%" class="theField">Link URL :</td>
				<td width="62%"><input name="url" value="<?php echo _getEcho($data,'url') ?>" type="text" class="text small require" id="url" size="30" maxlength="255" /></td>
			</tr>
			<tr>
				<td class="theField"> Link Name :</td>
				<td>
					<input name="name" value="<?php echo _getEcho($data,'name') ?>" type="text" class="text small require" id="name" size="30" maxlength="30" /></td>
			</tr>
			<tr>
				<td class="theField">Little Description :</td>
				<td><input name="desc" value="<?php echo _getEcho($data,'desc') ?>" type="text" class="text small require" id="desc" size="30" maxlength="255" /></td>
			</tr>
			<tr>
				<td class="theField"> Zipcode for Ad :</td>
				<td><input name="zipcode" value="<?php echo _getEcho($data,'zipcode') ?>" type="text" class="text small require" id="zipcode" size="30" maxlength="10" onkeypress="return isNumberKey(event)" /></td>
			</tr>
			<tr>
				<td class="theField">Your email address :</td>
				<td><input name="email" value="<?php echo _getEcho($data,'email') ?>" type="text" class="text small require" id="email" size="30" maxlength="150" /></td>
			</tr>
			<tr>
				<td></td>
				<td>
					<input name="Submit" type="submit" class="submit mid" value=" Update "/>
					<input name="Reset" type="reset" class="submit mid" value=" Reset "/>
				</td>
			</tr>
		</table>

	</form>
</p>
</div>
<!-- .block_content ends -->
</div>
<!-- .block ends -->
<!-- .block ends -->
<!-- .block.small.left ends -->
<!-- .block.small.right ends -->
<!-- .block ends -->
<!-- .block ends -->
<!-- .leftcol ends -->
<?php include("__footer.php"); ?>
</div>
<!-- wrapper ends -->

</div>
<!-- #hld ends -->
</body>
</html>